package com.ly.wxPay.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.wxPay.bean.entity.WxRefund;
import com.ly.wxPay.enums.WxRefundStatus;
import com.ly.wxPay.mapper.WxRefundMapper;
import com.ly.wxPay.service.IWxRefundService;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 支付-退款表-impl
 *
 * @author luoyun
 * @Date: 2025-11-22
 */
@Service
public class WxRefundServiceImpl extends ServiceImpl<WxRefundMapper, WxRefund> implements IWxRefundService {

    @Override
    public R getId(String id) {
        WxRefund entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(WxRefund entity) {
        List<WxRefund> list = this.list(
                new LambdaQueryWrapper<WxRefund>()
                        .orderByDesc(WxRefund::getCreateTime)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(WxRefund entity, Page<WxRefund> page) {
        IPage<WxRefund> pageList = this.page(
                page,
                new LambdaQueryWrapper<WxRefund>()
                        .orderByDesc(WxRefund::getCreateTime)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(WxRefund entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(WxRefund entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<WxRefund> dto) {
        List<WxRefund> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public void updateRefund(String content) {
        HashMap<String, String> resultMap = JSONUtil.toBean(content, HashMap.class);
        WxRefund entity = new WxRefund();
        entity.setRefundId(resultMap.get("refund_id"));// 微信支付退款单号
        // 查询退款和申请退款中的返回参数
        if (resultMap.get("status") != null) {
            entity.setRefundStatus(resultMap.get("status"));// 退款状态
            entity.setContentReturn(content);// 将全部响应结果存入数据库的content字段
        }
        // 退款回调中的回调参数
        if (resultMap.get("refund_status") != null) {
            entity.setRefundStatus(resultMap.get("refund_status"));// 退款状态
            entity.setContentNotify(content);// 将全部响应结果存入数据库的content字段
        }
        this.update(entity,
                new LambdaUpdateWrapper<WxRefund>()
                        .eq(WxRefund::getRefundNo, resultMap.get("out_refund_no"))
        );
    }

    @Override
    public List<WxRefund> getNoRefundOrderByDuration(int minutes) {
        // minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        return this.list(
                new LambdaQueryWrapper<WxRefund>()
                        .eq(WxRefund::getRefundStatus, WxRefundStatus.PROCESSING.getType())
                        .le(WxRefund::getCreateTime, instant)
        );
    }
}
